home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c
- Subject: Re: Probs with prototype mixes with Functions
- Date: 20 Apr 1996 08:15:44 GMT
- Organization: systems hk
- Message-ID: <4la6fg$7tq@nadine.teleport.com>
- References: <dward.1.000DC9CA@melbpc.org.au>
- NNTP-Posting-Host: ip-pdx03-45.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- dward@melbpc.org.au (Darren Ward) wrote:
- >when making a function such as:
- >
- >float convert(int celcius)
- >{
- > float faranheit;
- > faranheit = ((float)celcius*9/5)+32;
- > return faranheit;
- >}
- >and it is called by the main program as
- > faran = convert(celc);
- >where faran is a float and celc is an int.
- >
- >The problem is that I can't compile it as it keeps giving me errors on return
- >type but as far as I can see the main is waiting for a float and the function
- >should return a float?
-
- Darren,
-
- If the call to 'convert' precedes the declaration of 'convert' as
- a function returning a 'float', does the compiler not assume it
- returns an 'int', since you have no prototype up above stating
- otherwise? I get confused by the behavior, since it seems to me
- that the response is usually a run-time error rather than a compile
- time error, unless you're using a C++ compiler. For instance, if
- you leave out the 'math.h' header, and use 'sqrt()' without the
- header's prototype, there is usually no objection during compile,
- but plenty of objection to your results (trying to use the returned
- integer bit-pattern as a floating point number). By the way, I also
- think that there is no guarantee that your division of '9/5' will
- be what you want, even though you are casting the 'celcius' in
- front of it, since they (9 & 5) are both integers, and may or may not
- return '1' as a result.
-
- Yours, Geoff Houck
-
-